package com.corpgovernment.organization.controller;

import com.corpgovernment.api.applytrip.enums.ApplyTripEmployeeEnum;
import com.corpgovernment.api.organization.model.user.NameInfoBo;
import com.corpgovernment.api.organization.model.user.nonemployee.*;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.organization.entity.db.MbFreshPassenger;
import com.corpgovernment.organization.entity.db.MbFreshPassengerRelation;
import com.corpgovernment.organization.entity.db.MbOrgNonEmployeeInfo;
import com.corpgovernment.organization.service.IFreshPassengerService;
import com.corpgovernment.organization.service.IOrganizationNonEmployeeService;
import com.corpgovernment.organization.service.impl.FreshPassengerRelationServiceImpl;
import com.corpgovernment.organization.util.StrUtils;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 非员工信息控制器
 *
 * @author wangyujue
 */
@RestController
@RequestMapping("/nonEmployee")
@Slf4j
public class OrganizationNonEmployeeController {

    @Autowired
    private IOrganizationNonEmployeeService organizationNonEmployeeInfoService;
    @Autowired
    private IFreshPassengerService freshPassengerService;
    @Autowired
    private FreshPassengerRelationServiceImpl freshPassengerRelationService;

    /**
     * 保存非员工
     *
     * @param orgNonEmployeeVo
     * @return
     */
    @RequestMapping("/save")
    JSONResult<Long> save(@RequestBody OrgNonEmployeeVo orgNonEmployeeVo) {
        log.info("调用保存非员工接口[{}]", JsonUtils.toJsonString(orgNonEmployeeVo));
        MbOrgNonEmployeeInfo mbOrgNonEmployeeInfo = new MbOrgNonEmployeeInfo();
        BeanUtils.copyProperties(orgNonEmployeeVo, mbOrgNonEmployeeInfo);
        if (Objects.isNull(mbOrgNonEmployeeInfo.getEmployeeType())){
            mbOrgNonEmployeeInfo.setEmployeeType(ApplyTripEmployeeEnum.EXTERNAL_EMPLOYEE.getCode());
        }
        mbOrgNonEmployeeInfo.setEmail(orgNonEmployeeVo.getMail());
        mbOrgNonEmployeeInfo.setNationality(orgNonEmployeeVo.getNationality());
        NameInfoBo nameInfoBo = JsonUtils.parse(JsonUtils.toJsonString(orgNonEmployeeVo),NameInfoBo.class);
        if(StrUtils.checkNameInfo(nameInfoBo)){
            mbOrgNonEmployeeInfo.setNameInfo(JsonUtils.toJsonString(nameInfoBo));
        }
        // 保存非员工
        organizationNonEmployeeInfoService.save(mbOrgNonEmployeeInfo);
        MbFreshPassenger mbFreshPassenger = new MbFreshPassenger();
        mbFreshPassenger.setNonEmployeeUid(mbOrgNonEmployeeInfo.getId());
        mbFreshPassenger.setCreateUid(mbOrgNonEmployeeInfo.getEmployeeUid());
        mbFreshPassenger.setName(mbOrgNonEmployeeInfo.getName());
        if (StringUtils.isNotBlank(orgNonEmployeeVo.getFirstEnName()) && StringUtils.isNotBlank(orgNonEmployeeVo.getLastEnName())) {
            mbFreshPassenger.setEnName(orgNonEmployeeVo.getFirstEnName() + "/" +orgNonEmployeeVo.getLastEnName());
        }
        // 保存新鲜度表非员工数据
        freshPassengerService.save(mbFreshPassenger);
        //维护常用旅客信息
        if(orgNonEmployeeVo.getRelationFlag()!=null&&orgNonEmployeeVo.getRelationFlag()) {
            MbFreshPassengerRelation freshPassengerRelation = new MbFreshPassengerRelation();
            Date date = new Date();
            freshPassengerRelation.setDatachangeCreatetime(date);
            freshPassengerRelation.setDatachangeLasttime(date);
            freshPassengerRelation.setIsDeleted(false);
            freshPassengerRelation.setUid(orgNonEmployeeVo.getEmployeeUid());
            freshPassengerRelation.setFreshTime(date);
            freshPassengerRelation.setEmployeeFlag(0);
            freshPassengerRelation.setFreshUid(String.valueOf(mbOrgNonEmployeeInfo.getId()));
            freshPassengerRelationService.save(Arrays.asList(freshPassengerRelation));
        }
        return new JSONResult<>(mbOrgNonEmployeeInfo.getId());
    }


    /**
     * 分页查询员工信息
     * http://localhost:8989/org/nonEmployee/searchOrgUserInfoPage?employeeUid=_SL002
     *
     * @param search
     * @return
     */
    @RequestMapping("/searchOrgUserInfoPage")
    public JSONResult searchOrgUserInfoPage(SearchOrgNonEmployeePageRequest search) {
        // 几个查询条件不能同时为空，要不然就是扫描全表查询
        return JSONResult.success(organizationNonEmployeeInfoService.page(search));
    }

    /**
     * 查询单个用户信息
     * http://localhost:8989/org/nonEmployee/getOrgUserInfo?id=_SL002
     *
     * @param request
     * @return
     */
    @RequestMapping("/getOrgUserInfo")
    public JSONResult<GetOrgNonEmployeeResponse> getOrgUserInfo(@RequestBody GetOrgNonEmployeeRequest request) {
        // 查询非员工信息
        GetOrgNonEmployeeResponse getOrgNonEmployeeResponse = organizationNonEmployeeInfoService.find(request);
        return JSONResult.success(getOrgNonEmployeeResponse);
    }


    @RequestMapping("/getOrgUserInfoList")
    public JSONResult<List<GetOrgNonEmployeeResponse>> getOrgUserInfoList(@RequestBody GetOrgNonEmployeeRequest request) {
        // 查询非员工信息
        List<MbOrgNonEmployeeInfo> mbOrgNonEmployeeInfos = organizationNonEmployeeInfoService.listByIds(request.getIds());
        if (CollectionUtils.isNotEmpty(mbOrgNonEmployeeInfos)){
            List<GetOrgNonEmployeeResponse> result = new ArrayList<>(mbOrgNonEmployeeInfos.size());
            for (MbOrgNonEmployeeInfo mbOrgNonEmployeeInfo:mbOrgNonEmployeeInfos){
                GetOrgNonEmployeeResponse getOrgNonEmployeeResponse = new GetOrgNonEmployeeResponse();
                BeanUtils.copyProperties(mbOrgNonEmployeeInfo, getOrgNonEmployeeResponse);
                result.add(getOrgNonEmployeeResponse);
            }
            return JSONResult.success(result);
        }
        return JSONResult.success(Collections.emptyList());
    }

    /**
     * 创建或者更新用户信息
     * http://localhost:8989/org/nonEmployee/createOrUpdateOrgUserInfo?id=_SL002
     *
     * @param request
     * @return
     */
    @PostMapping("/createOrUpdateOrgUserInfo")
    public JSONResult createOrUpdateOrgUserInfo(CreateOrUpdateOrgNonEmployeeRequest request) {
        return JSONResult.success(organizationNonEmployeeInfoService.createOrUpdate(request));
    }

    /**
     * 删除用户信息
     * http://localhost:8989/org/nonEmployee/deleteOrgUserInfo?id=_SL002
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteOrgUserInfo/{id}")
    public JSONResult deleteOrgUserInfo(@PathVariable Long id) {
        int result = organizationNonEmployeeInfoService.delete(id);
        return result > 0 ? JSONResult.ok() : JSONResult.errorMsg("删除失败");
    }


}
